﻿using System.Collections.Generic;

namespace Nomo.CoreModule
{
    /// <summary>
    /// 字典池。
    /// </summary>
    /// <typeparam name="TKey">字典的键的类型。</typeparam>
    /// <typeparam name="TValue">字典的值的类型。</typeparam>
    public static class DictionaryPool<TKey, TValue>
    {
        private static readonly ObjectPool<Dictionary<TKey, TValue>> Pool = new ObjectPool<Dictionary<TKey, TValue>>(() => new Dictionary<TKey, TValue>(), default, e => e.Clear());

        /// <summary>
        /// 从字典池里取出一个空字典。
        /// </summary>
        /// <returns>一个空字典。</returns>
        public static Dictionary<TKey, TValue> Get()
        {
            return Pool.Get();
        }

        /// <summary>
        /// 将一个空字典放回字典池。
        /// </summary>
        /// <param name="dictionary">要放回字典池的字典。</param>
        public static void Release(Dictionary<TKey, TValue> dictionary)
        {
            if (dictionary != null)
            {
                Pool.Release(dictionary);
            }
        }

        /// <summary>
        /// 移除字典池中的所有字典。
        /// </summary>
        public static void Clear()
        {
            Pool.Clear();
        }
    }
}
